$(document).ready(function() { 
	var forms = document.getElementsByTagName("form");		 
	if(forms && forms[0] && forms[0].getAttribute("name")){	   
		var menuCodeHidden = "<input type='hidden' name='menuCode' id='hiddenMenuCode'  value='"+menuCodeValue+"' />";
		var formName = forms[0].getAttribute("name");
		$("#"+formName).append(menuCodeHidden);
		//检测form内所有text元素的字符合法性 	 
	}
	
	$("input[type='text']:not(:disabled)").blur(function(){		
		$("input[type='text']:not(:disabled)").each(function(){
			$(this).val($.trim($(this).val()));
		});
	});
	uploader();
	/*页面类型*/
//	var pageType = "${pageType}";
	if(pageType && pageType === "detailInfo"){
		$("input, textarea, select").attr("readonly", "readonly");
		$(".detail-remove").remove();
	}
	/*记录初始化时的日期*/
	$(".easyui-datebox").each(function(i, datebox){
		$(datebox).data("init", datebox.value);
	});
});

/*按键监听事件*/
$(document).keydown(function(e){
	/*回车事件*/
	if(13 === e.keyCode){
		doSearch();
	}
});

function back(){
//	window.history.back(-1);
	window.location.href = document.referrer;
}
 
/**
 * 替换字符串的第一个逗号
 * @param ids
 * @return
 */
function replaceFirst(ids){
	var regExp = /(^,)|(,$)/gi;
	ids = "" + ids;
	var tempids = ids.replace(regExp, "");
	return tempids;
}

/**
 * 通用附件下载方法
 * @param url
 * @param formId
 * @return
 */
function commonFileDownload(url,formId){
	 $.fileDownload(url, {
	        preparingMessageHtml: "正在导出EXCEL，请稍等...",
	        failMessageHtml: "导出失败，请重试",
	        httpMethod: "POST",
	        failCallback:fileRspDownLoadMsg,
	        data: $("#"+formId).serialize()
	    });
}
/**
 * 下载回调方法
 * @param msg
 * @return
 */
function fileRspDownLoadMsg(msg){
	alert(msg);
}

//打开模态框
function openModal(el, options, callback){
	return $(el).jpwin(options, callback);
}

function resetForm(){
	$(".easyui-textbox").textbox("setValue", "");
	$("select").not(".pagination-page-list").each(function(){
	       var myid=$(this).attr("id");
	       $("#"+myid+" option:first").prop("selected", 'selected');	       
	});
	//重置下拉框
	$(".easyui-combobox").each(function(){
		var data= $(this).combobox("getData");
		if (data.length > 0) {
            $(this).combobox('select', data[0].id);
        }
	});
	/*初始化日期取出*/
	$(".easyui-datebox").each(function(i, datebox){
		$(datebox).datebox('setValue', $(datebox).data("init"));
	});
}

/**
 * 通用增加修改方法
 * @param url 添加或修改提交的url地址
 * @param type 操作类型 update修改，audit审核,inquery查看,save新增
 * @param tableId 表格id
 * @return
 */
function commonDetail(url,type,tableId){
	var ids = "";	
	var temp = true;
	if(type == "update" || type == "audit" || type == "inquery"|| type == "detail"){
		var checkedItems = checkSelected(tableId, true);
		if(!checkedItems){
			return false;
		}
		ids = (checkedItems[0]).UUID;
	}else{
		ids = "";
	}
 	if(temp){
 		//window.location.href=url+ids+"&menuCode="+menuCodeValue;
 		if(url.indexOf("?") != -1){
			url = url+ ids+ "&menuCode=" + menuCodeValue;
		}else{
			url = url+ ids+ "?menuCode=" + menuCodeValue;
		}
		window.location.href=url; 
 	}
}

/*通用详情方法*/
function detailInfo(url){
	var preurl = url.substring(0, url.indexOf("?") + 1);
	var tableId = url.substr(url.indexOf("?") + 1);
	url = preurl + "pageType=detailInfo&" + tableId;
	tableId = tableId.substring(4, tableId.indexOf("&"));
	commonDetail(url, "detail", tableId);
}
/**
 * 通用删除方法
 * @param url 添加或修改提交的url地址
 * @param type 操作类型 update修改，audit审核,inquery查看,save新增
 * @param tableId 表格id
 * @return
 */
function commonDel(url,type,tableId,msg){
	var checkedItems = checkSelected(tableId, false);
	if(!checkedItems){
		return false;
	};
	var ids = "";
	for(var i = 0 ; i < checkedItems.length; i++){
		ids = ids + "," + (checkedItems[i]).UUID;
	}
	ids = replaceFirst(ids);
	if(!msg || msg ==null || msg == '' ){
		msg = "确定删除所选择的记录吗?";
	}
	
	$.jpmessage.confirm(msg, function(r){
		if (r){
			if(url.indexOf("?") != -1){
				url = url+ ids+ "&menuCode=" + menuCodeValue;
			}else{
				url = url+ ids+ "?menuCode=" + menuCodeValue;
			}
			window.location.href=url; 
		}
	});
}
 
/**
 * 通用批量处理方法
 * @param url 添加或修改提交的url地址
 * @param type 操作类型 update修改，audit审核,inquery查看,save新增
 * @param tableId 表格id
 * @param msg 提示消息
 * @return
 */
function commonBatch(url,tableId,msg){
	var checkedItems = checkSelected(tableId, false);
	if(!checkedItems){
		return false;
	};
	var ids = "";
	for(var i = 0 ; i < checkedItems.length; i++){
		ids = ids + "," + (checkedItems[i]).UUID;
	}
	ids = replaceFirst(ids);	
	$.jpmessage.confirm(msg+"?", function(r){
		if (r){
			if(url.indexOf("?") != -1){
				url = url+ ids+ "&menuCode=" + menuCodeValue;
			}else{
				url = url+ ids+ "?menuCode=" + menuCodeValue;
			}
			window.location.href=url; 
		}
	});
}

/**
 * 异步批量处理方法
 * @param url 操作地址
 * @param tableId 表格id
 * @param msg 询问提示信息
 * @param success 成功回调，处理后续业务
 * @returns
 */
function commonAsync(url, tableId, msg, success){
	var checkedItems = checkSelected(tableId, false);
	if(!checkedItems){
		return false;
	};
	var ids = "";
	for(var i = 0 ; i < checkedItems.length; i++){
		ids = ids + "," + (checkedItems[i]).UUID;
	}
	ids = replaceFirst(ids);	
	$.jpmessage.confirm(msg + "?", function(r){
		if(r){
			$.jpajax({
				url: url + ids,
				success: success
			});
		}
	});
}
 
/**
 * 通用表单提交方法
 * @param formId form的Id
 * @param url 请求地址
 */
function submitForm(formId,url){  
	var valForm = $('#'+formId).form('enableValidation').form('validate');
	if(valForm){
	/*	 $.blockUI({ css : {  
             border : "none",    
             padding : "15px",    
             backgroundColor : "#000",    
             "-webkit-border-radius" : "10px",    
             "-moz-border-radius" : "10px",    
             opacity : .5,    
             color : "#fff"    
         },  
         message : "正在处理中,请稍等。。。" ,
         baseZ:2000
		 })  ;*/
		$.messager.progress({ 
			title:'请稍后',  
		    msg: '正在处理中,请稍等。。。' 
		});
	
		if(url && url!=null && url!="" ){
			$("#"+formId).attr("action", url);	
		}
		$('#'+formId).submit();
	}
}

/**
 * 检查是否有勾选列表数据
 * @param tableId 表格id
 * @param isSingle 是否限制勾选单条
 * @returns
 */
function checkSelected(tableId, isSingle){
	tableId = tableId || "gridTable";
	var msg, checkedItems = $('#'+tableId).datagrid('getChecked');
	if(checkedItems.length == 0){
		msg = "请至少选择一条记录!";
	}else if(isSingle && checkedItems.length > 1){
		msg = "只能选择一条记录!";
	}
	if(msg){
		$.jpmessage.warning(msg);
		return false;
	}
	return checkedItems;
}

/**
 * 展示机构树
 * @param confirmCallback 弹窗确定按钮回调函数
 * @param uuid 节点展开属性ID的值
 * @returns 树对象
 */
function showInsTree(confirmCallback, uuid, option){
	var options = {
		url: "../insinf/getInsInfTree.do",
		title: "选择机构",
		confirmCallback: confirmCallback,
		openIds: uuid,
		showSearch: true,
		searchTitle: "机构代码/名称",
		width: 500,
		height: 500
	};
	options = $.extend({}, options, option);
	return showTree(options);
}
/**
 * 选择地区
 * @param confirmCallback
 * @param uuid
 * @param option
 * @returns
 */
function showAreaTree(confirmCallback, uuid, option){
	var options = {
		url: "../areacd/getAreaTree.do",
		title: "选择地区",
		confirmCallback: confirmCallback,
		openIds: uuid
	};
	options = $.extend({}, options, option);
	return showTree(options);
}

function showTree(options){
	var jpTree = $().jpTree(options);
	return jpTree;
}
/**
 * 上传图片
 * @param el
 * @param option
 * @returns
 */
//function uploader(el, option){
//	new JpUploader($(el), option);
//}
//
//function uploader(el, uploadType, uploadName, affixType, storeType, affixUuid){
//	var option = {
//		uploadType: uploadType,
//		uploadName: uploadName,
//		affixType: affixType,
//		storeType: storeType,
//		affixUuid: affixUuid
//	}
//	uploader(el, option);
//}

function uploader(){
	$.each($(".uploader-file"), function(i, obj){
		$(this).append('<span id="imgContainer'+i+'" class="uploader-list detail-remove"></span>');
		$(this).append('<span id="uploadbtn'+i+'" class="uploader-list detail-remove">选择图片</span>');
		var formData = {
			uploadType: $(this).attr("uploadType"),
			uploadName: $(this).attr("uploadName"),
			affixType: $(this).attr("affixType"),
			storeType: $(this).attr("storeType"),
			affixIdx: $(this).attr("affixIdx")
		};
		var options = {
			formData: formData,
			imgEl: $(this).parent(),
			pick: "#uploadbtn"+i
		};
		new JpUploader($(this), options);
	});
}

/**
 * 预览图片
 * @param els
 * @returns
 */
function viewImg(els){
	$(els).viewer();
}